﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        function Check() {
            if (!document.getElementById("txt").value) {
                alert("不可为空!");
                return false;
            }
            return true;
        }
        function Check2(e) {
            var b;
            if (!document.getElementById("txt").value) {
                alert("不可为空!");
                b = false;
            }
            else {
                b = true;
            }

            if (!b) {
                if (window.event) {
                    // IE中取消事件
                    window.event.returnValue = false;
                }
                else {
                    // w3c ff 取消事件
                    e.preventDefault();
                }
            }
        }
        window.onload = function () {
            //document.getElementById("sub").onclick = Check;
            if (window.attachEvent) {//IE 8及以前
                document.getElementById("sub").attachEvent("onclick", Check2);
            }
            else {//w3c 火狐
                document.getElementById("sub").addEventListener("click", Check2, false);
            }

        };

        function TestClick() {
            // 事件模拟
            document.getElementById("sub").click();
        }

    </script>
</head>
<body>
    <form onsubmit="alert('提交成功!');">
    <input id="txt" type="text" onclick="TestClick();"  />
    <!--元素中 return false-->
    <!--<input id="sub" type="submit" onclick="return Check();" value="提交" />-->
    <input id="sub" type="submit" value="提交" />
    </form>
</body>
</html>
